package com.example.demo.ds;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MyList<T> {

    private Object[] elem;
    private int usedSize;//0
    //默认容量
    private static final int DEFAULT_SIZE = 10;

    public MyList() {
        this.elem = new Object[DEFAULT_SIZE];
    }

    public MyList(int size) {
        if (size <= 0) {
            throw new IndexException("MyArraylist");
        }
        this.elem = new Object[size];
    }

    /**
     * 打印顺序表:
     * 根据usedSize判断即可
     */
    public void display() {
        if(isEmpty()) {
            System.out.println("[]");
        }
        else{
            System.out.print("[ ");
            for (int i = 0; i < this.usedSize; i++) {
                if(i == usedSize - 1) {
                    System.out.print(this.elem[i] + " ]");
                }else {
                    System.out.print(this.elem[i] + ", ");
                }
            }
            System.out.println();
        }
    }

    // 新增元素,默认在数组最后新增
    public void add(T data) {
        if (isFull()) {
            elem = Arrays.copyOf(elem, 2 * usedSize);
        }
        elem[usedSize] = data;
        usedSize++;
    }
    /**
     * 判断当前的顺序表是不是满的！
     *
     * @return true:满   false代表空
     */

    public boolean isFull() {
        if (usedSize == elem.length) {
            return true;
        } else {
            return false;
        }
    }


    private boolean checkPosInAdd(int pos) {
        if (pos < 0 || pos > this.usedSize) {
            return false;
        }
        return true;//合法
    }

    // 在 pos 位置新增元素
    public void add(int pos, T data) {
        if (this.isFull()) {
            elem = Arrays.copyOf(elem, 2 * usedSize);
        }
        if (checkPosInAdd(pos)) {
            usedSize++;
            for (int i = usedSize; i > pos; i--) {
                elem[i] = elem[i - 1];
            }
            elem[pos] = data;
        }else {
            throw new IndexException("add");
        }
    }

    // 判定是否包含某个元素
    public boolean contains(T toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (elem[i].equals(toFind)) {
                return true;
            }
        }
        return false;
    }

    // 查找某个元素对应的位置
    public int indexOf(T toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (elem[i].equals(toFind)) {
                return i;
            }
        }
        return -1;
    }

    // 获取 pos 位置的元素
    public T get(int pos) {
        if (checkPosInAdd(pos)) {
            if (pos == usedSize) {
                throw new IndexException("get");
            } else {
                return (T)elem[pos];
            }
        } else {
            throw new IndexException("get");
        }
    }

    private boolean isEmpty() {
        return usedSize == 0;
    }

    // 给 pos 位置的元素设为【更新为】 value
    public void set(int pos, T value) {
        if (checkPosInAdd(pos)) {
            if (pos == usedSize) {
                throw new IndexException("set");
            } else {
                this.elem[pos] = value;
            }
        } else {
            throw new IndexException("set");
        }
    }

    /**
     * 删除第一次出现的关键字key
     *
     * @param key
     */
    public void remove(T key) {
        int index = indexOf(key);
        if (index == -1) {
            System.out.println("Can‘t find");
        } else {
            for (int i = index; i < usedSize - 1; i++) {
                this.elem[i] = this.elem[i + 1];
            }
            usedSize--;
        }
    }

    // 获取顺序表长度
    public int size() {
        return usedSize;
    }

    // 清空顺序表
    public void clear() {
        this.elem = new Object[DEFAULT_SIZE];
        this.usedSize = 0;
    }

    // 升级成List
    public List<T> upgrade() {
        List<T> list = new ArrayList<>();
        for (int i = 0; i < usedSize; i++) {
            list.add((T)elem[i]);
        }
        return list;
    }

    @Override
    public String toString() {
        return "MyList{" +
                "elem=" + Arrays.toString(elem) +
                '}';
    }
}

